package cn.itcast.recycleviewdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private RecyclerView recyclerView;
    private List<GoodsBean> recommendList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();

        recyclerView = findViewById(R.id.recommend_recycle);

        //https://blog.csdn.net/a8688555/article/details/79634295
        //Item的高度是固定的，设置这个选项可以提高性能.总得来说就是就是避免整个布局绘制。就是避免requestLayout
        recyclerView.setHasFixedSize(true);

        //设置普通布局
        //  LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //recyclerView.setLayoutManager(layoutManager);
        //设置瀑布流布局为2列，垂直方向滑动
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        GoodsAdapter goodsAdapter = new GoodsAdapter(this, recommendList);
        recyclerView.setAdapter(goodsAdapter);
        goodsAdapter.setClick(new GoodsAdapter.IMyClickListener() {
            @Override
            public void onClickTest(int i) {
                Toast.makeText(MainActivity.this,"main  click"+i,Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){

            case R.id.StaggeredGridLayoutManager:

                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

                break;

            case R.id.LinearLayoutManager:

                recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));


                break;

            case R.id.GridLayoutManager:

                recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));

                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 数据源
     */
    private void initData() {
        String[] names = new String[]{"唐诗宋词", "京剧文化", "传统美食", "近代工艺", "民间习俗", "京剧文化", "传统美食", "近代工艺", "民间习俗"};
        int[] ImageId = new int[]{R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i};
        for (int i = 0; i < names.length; i++) {
            this.recommendList.add(new GoodsBean(names[i], ImageId[i]));
        }
    }
}
